package com.club.football.controller;

import com.club.football.dto.NewsSaveDto;
import com.club.football.dto.PageDto;
import com.club.football.entity.Coach;
import com.club.football.entity.News;
import com.club.football.req.NewsPageReq;
import com.club.football.service.NewsService;
import com.club.football.utils.FileUtils;
import com.club.football.utils.R;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import static com.club.football.common.CommonConstants.DEL_OK_MSG;
import static com.club.football.common.CommonConstants.GET_INFO_OK;

/**
 * 新闻资讯Controller
 *
 * @author 1
 * @date 2023/12/28
 */
@RestController
@RequestMapping("/news")
public class NewsController {


    @Resource
    private NewsService newsService;


    /**
     * 页面
     *
     * @param pageDto 页数到页
     * @return {@link R}
     */
    @GetMapping("/page")
    public R<PageDto<News>> page(PageDto<News> pageDto, NewsPageReq newsPageReq) {
        return newsService.newsPage(pageDto, newsPageReq);
    }

    /**
     * 保存
     *
     * @return {@link R}<{@link String}>
     */
    @PostMapping("/save")
    public R<String> save(@RequestBody @Valid NewsSaveDto saveDto) {
        return newsService.newsSave(saveDto);
    }

    /**
     * 删除
     *
     * @param id ID
     * @return {@link R}<{@link String}>
     */
    @DeleteMapping("/remove/{id}")
    public R<String> remove(@PathVariable Long id) {
        newsService.removeById(id);
        return R.ok(DEL_OK_MSG);
    }

    /**
     * 信息
     *
     * @param id ID
     * @return {@link R}<{@link Coach}>
     */
    @GetMapping("/info/{id}")
    public R<News> info(@PathVariable Long id) {
        News news = newsService.getById(id);
        news.setImage(FileUtils.getUrl(news.getImage()));
        return R.ok(news, GET_INFO_OK);
    }
}
